package me.zhengjie.modules.equipment.service.impl;

import me.zhengjie.modules.equipment.domain.TEqpttype;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.equipment.repository.TEqpttypeRepository;
import me.zhengjie.modules.equipment.service.TEqpttypeService;
import me.zhengjie.modules.equipment.service.dto.TEqpttypeDTO;
import me.zhengjie.modules.equipment.service.dto.TEqpttypeQueryCriteria;
import me.zhengjie.modules.equipment.service.mapper.TEqpttypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

/**
* @author sw
* @date 2019-07-19
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TEqpttypeServiceImpl implements TEqpttypeService {

    @Autowired
    private TEqpttypeRepository tEqpttypeRepository;

    @Autowired
    private TEqpttypeMapper tEqpttypeMapper;

    @Override
    public Object queryAll(TEqpttypeQueryCriteria criteria, Pageable pageable){
        Page<TEqpttype> page = tEqpttypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tEqpttypeMapper::toDto));
    }

    @Override
    public Object queryAll(TEqpttypeQueryCriteria criteria){
        return tEqpttypeMapper.toDto(tEqpttypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public TEqpttypeDTO findById(Integer id) {
        Optional<TEqpttype> tEqpttype = tEqpttypeRepository.findById(id);
        ValidationUtil.isNull(tEqpttype,"TEqpttype","id",id);
        return tEqpttypeMapper.toDto(tEqpttype.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TEqpttypeDTO create(TEqpttype resources) {
        return tEqpttypeMapper.toDto(tEqpttypeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TEqpttype resources) {
        Optional<TEqpttype> optionalTEqpttype = tEqpttypeRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalTEqpttype,"TEqpttype","id",resources.getId());

        TEqpttype tEqpttype = optionalTEqpttype.get();
        // 此处需自己修改
        resources.setId(tEqpttype.getId());
        tEqpttypeRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        tEqpttypeRepository.deleteById(id);
    }
}